import { Body, Controller, Post, Get, Req } from '@nestjs/common';
import { ApiOkResponse, ApiOperation, ApiSecurity, ApiTags } from '@nestjs/swagger';
import { ADMIN_PREFIX } from '../../admin.constants';
import { AdminUser } from '../../core/decorators/admin-user.decorator';
import { SysLabelTemplateService } from './labelTemplate.service';
import { FastifyRequest } from 'fastify';

@ApiSecurity(ADMIN_PREFIX)
@ApiTags('标签模板配置模块')
@Controller('labelTemplate')
export class SysLabelTemplateController {
  constructor(private labelTemplateService: SysLabelTemplateService) {}

  @ApiOperation({ summary: '分页查询标签模板列表' })
  @ApiOkResponse()
  @Post('page')
  async page(@Body() dto) {
    const [list, total] = await this.labelTemplateService.page(dto);
    return {
      list,
      pagination: {
        total,
        pageNum: dto.pageNum,
        pageSize: dto.pageSize,
      },
    };
  }

  @ApiOperation({ summary: '添加标签模板' })
  @ApiOkResponse()
  @Post('add')
  async add(@Body() dto, @AdminUser() user, @Req() req: FastifyRequest) {
    const { mac = '', ip = '', synchostsqlite: syncHostSqlite = '' } = req.headers;
    await this.labelTemplateService.isExistKey(dto.tempName);
    await this.labelTemplateService.add(dto, user, mac, ip, syncHostSqlite);
  }

  @ApiOperation({ summary: '更新标签模板' })
  @ApiOkResponse()
  @Post('update')
  async update(@Body() dto, @AdminUser() user, @Req() req: FastifyRequest) {
    const { mac = '', ip = '', synchostsqlite: syncHostSqlite = '' } = req.headers;
    await this.labelTemplateService.update(dto, user, mac, ip, syncHostSqlite);
  }

  @ApiOperation({ summary: '删除标签模板' })
  @ApiOkResponse()
  @Post('delete')
  async delete(@Body() dto, @AdminUser() user, @Req() req: FastifyRequest) {
    const { mac = '', ip = '', synchostsqlite: syncHostSqlite = '' } = req.headers;
    await this.labelTemplateService.delete(dto, user, mac, ip, syncHostSqlite);
  }

  @ApiOperation({ summary: '根据id获取标签模板详情' })
  @ApiOkResponse()
  @Post('info')
  async info(@Body() dto) {
    return await this.labelTemplateService.info(dto);
  }

  @ApiOperation({ summary: '设置默认标签模板' })
  @ApiOkResponse()
  @Post('setDefault')
  async setDefault(@Body() dto, @AdminUser() user) {
    await this.labelTemplateService.setDefault(dto, user);
  }

  @ApiOperation({ summary: '获取默认标签模板' })
  @ApiOkResponse()
  @Get('getDefault')
  async getDefault() {
    return await this.labelTemplateService.getDefault();
  }
}
